import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_house_manager/pages/controller/build_controller.dart';
import 'package:get/get.dart';

class RoomList extends StatefulWidget {
  const RoomList({super.key});

  @override
  State<RoomList> createState() => _RoomListState();
}

class _RoomListState extends State<RoomList> {
  BuildController buildController = Get.find<BuildController>();
  List roomList = [];

  /// 造数据
  fakeData() {
    // 1. 随机产生房间的数据 5-10
    int roomCount = Random().nextInt(6) + 5;

    // 2. 遍历产生楼栋号和房间号
    // 1003  403
    List rooms = [];
    for (int i = 0; i < roomCount; i++) {
      // 2.1 随机产生楼栋号 1-10
      int building = Random().nextInt(10) + 1;
      // 2.2 随机产生房间号
      int room = Random().nextInt(4) + 1;
      // 2.3 组合房间号
      String roomNum = '${building}0$room';
      // 2.4 去重
      if (rooms.contains(roomNum)) {
        continue;
      }
      // 2.5 加入到房间列表中
      rooms.add(roomNum);
    }
    setState(() {
      roomList = rooms;
    });
  }

  @override
  void initState() {
    super.initState();
    fakeData();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text('选择房间'),
        ),
        body: ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            return GestureDetector(
              onTap: () {
                // 将房间号存储到getx中
                buildController.buildInfo['room'] = roomList[index];
                buildController.updateBuildInfo(buildController.buildInfo);
                // 跳转到添加房屋页面
                Navigator.pushNamed(context, '/house_form');
              },
              child: Container(
                color: Colors.white,
                padding: const EdgeInsets.all(10),
                child: Row(
                  children: [
                    Expanded(
                        child: Text(
                            '${buildController.buildInfo['point']} ${buildController.buildInfo['building']} ${roomList[index]}室')),
                    const Row(children: [
                      Icon(Icons.arrow_forward_ios,
                          size: 16, color: Colors.black),
                    ])
                  ],
                ),
              ),
            );
          },
          itemCount: roomList.length,
        ));
  }
}
